//
//  HELCreditTableVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/2/21.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELCreditTableVC.h"
#import "HELNormalCell.h"
#import "HELCreditHeader.h"
#import "HELCreditModel.h"
@interface HELCreditTableVC ()

@property (nonatomic,  assign) BOOL isFirstEntry;



@end

@implementation HELCreditTableVC


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.isFirstEntry = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
  
    
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-49-200);
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELNormalCell" bundle:nil] forCellReuseIdentifier:@"HELNormalCell"];
//    self.tableView.dataVerticalOffset ;
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf getCreditList:YES];
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
        [weakSelf getCreditList:NO];
    }];
    
}

- (void)requestDataFirst{
    
    if (self.isFirstEntry) {
        [self getCreditList:YES];
        self.isFirstEntry = NO;
    }
    

}


- (void)getCreditList:(BOOL)isRecover{
    
    static NSUInteger page = 1;
    if (isRecover) {
        page =1;
    }else{
        page ++;
    }
    
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];

    self.tableView.loading = YES;
    params[@"page"] = @(page);
    params[@"rows"] = @(15);
    params[@"creditName"] = self.typeName;
    weakSelf_define;
    [[HELNetworkManager sharedMg]  POST:kCreditList parameters:params success:^(id responseObject) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage =[responseObject[@"data"][@"totalPage"] integerValue];
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELCreditModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            
            [weakSelf.tableView reloadData];
            
        }
        

    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return 49;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    HELNormalCell *cell =[tableView dequeueReusableCellWithIdentifier:@"HELNormalCell"];
    HELCreditModel *model = self.dataArray[indexPath.row];
    cell.mainTitleL.textColor = kTxtColor;
    cell.mainTitleL.text =model.courseName;
    cell.rightTitleL.textColor = [UIColor redColor];
    cell.rightTitleL.text = [NSString stringWithFormat:@"+%@",model.score];
    cell.rightTitleL.hidden = NO;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
